from pynput.keyboard import Key, Listener, KeyCode

backgroup = [[], [], []]
for i in range(0, len(backgroup)):
    backgroup[i] = [0, 0, 0]
backgroup[1][1] = 1


def on_write():
    for i in range(0, len(backgroup)):
        print(backgroup[i])


on_write()


def on_press(key):
    for i in range(0, len(backgroup)):
        for j in range(0, len(backgroup[i])):
            if backgroup[i][j] == 1:
                x = i
                y = j
                break
    if key == KeyCode.from_char('w'):
        print("向上移动")
        if x > 0:
            backgroup[x][y] = 0
            backgroup[x - 1][y] = 1
    if key == KeyCode.from_char('s'):
        print("向下移动")
        if x < 2:
            backgroup[x][y] = 0
            backgroup[x + 1][y] = 1
    if key == KeyCode.from_char('a'):
        print("向左移动")
        if y > 0:
            backgroup[x][y] = 0
            backgroup[x][y - 1] = 1
    if key == KeyCode.from_char('d'):
        print("向右移动")
        if y < 2:
            backgroup[x][y] = 0
            backgroup[x][y + 1] = 1
    on_write()


with Listener(on_press=on_press) as listener:
    listener.join()
